package com.authine.cloudpivot.ext.applicationservice.base;

import com.alibaba.cola.dto.RequestContext;
import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.MessageDto;
import com.authine.cloudpivot.ext.dto.system.DepartmentDto;
import com.authine.cloudpivot.ext.dto.system.UserDto;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.cloudpivot.ext.utils.system.DeptUtils;
import com.authine.cloudpivot.ext.utils.system.OrgUtils;
import com.authine.cloudpivot.ext.utils.system.UserUtils;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.common.TableHelper;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.org.dto.domainmodel.UnitType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ApplicationBaseService extends ApplicationService {
    private NamedParameterJdbcTemplate jdbcTemplate = ApplicationContextUtils.getBean(NamedParameterJdbcTemplate.class);
    private TableHelper tableHelper = ApplicationContextUtils.getBean(TableHelper.class);

    /**
     * 获取应用表名(可获取其他应用表名)
     *
     * @param appCode
     * @param schemaCode
     * @return
     */
    public String getTableName(String appCode, String schemaCode) {
        if (StringUtils.isBlank(appCode)) {
            getTableName(schemaCode);
        }
        return String.format("%s.i_%s_%s", appCode.toLowerCase(), appCode.toLowerCase(), schemaCode.toLowerCase());
    }

    /**
     * 获取表名称
     *
     * @param schemaCode
     * @return
     */
    public String getTableName(String schemaCode) {
        return tableHelper.getTableName(schemaCode);
    }

    /**
     * 修改消息
     *
     * @return
     */
    public SingleResponse<BO> updateMessage(MessageDto dto) throws RuntimeException {
        if (StringUtils.isBlank(dto.getId())) {
            throw new RuntimeException("id不能为空");
        }
        if (null==dto.getDeleted() && null== dto.getRead()) {
            throw new RuntimeException("删除，已读不能都为空");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", dto.getId());
        if(null!=dto.getDeleted()){
            map.put("del", dto.getDeleted());
        }
        if(null!=dto.getRead()){
            map.put("read", dto.getRead());
            map.put("read_name", dto.getRead()?"已读":"未读");
        }
        return BoServiceUtils.updateMainBo(ModelConst.T_MESSAGE, map);
    }
}
